package com.xjh.him.fragment.friend;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.xjh.him.R;
import com.xjh.him.adapter.FriendApplyAdapter;
import com.xjh.him.common.Constant;
import com.xjh.him.core.BaseFragment;
import com.xjh.him.databinding.FragmentFriendApplyBinding;
import com.xjh.him.dto.friendapply.FriendApplyDTO;
import com.xjh.him.dto.user.UserDTO;
import com.xjh.him.utils.MMKVUtils;
import com.xjh.him.utils.Utils;
import com.xjh.him.utils.XToastUtils;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.utils.WidgetUtils;

import org.xjh.tool.Checks;

import java.util.List;

@Page(name = "新的朋友")
public class FriendApplyFragment extends BaseFragment<FragmentFriendApplyBinding> {
    private FriendApplyAdapter mAdapter;
    private UserDTO userDTO;

    @NonNull
    @Override
    protected FragmentFriendApplyBinding viewBindingInflate(LayoutInflater inflater, ViewGroup container) {
        return FragmentFriendApplyBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
        //初始化 recyclerView
        WidgetUtils.initRecyclerView(binding.rvApplyList, 0);

        String json = (String) MMKVUtils.get(Constant.MMKV_KEY_USER_DTO, "");
        if (!Checks.isEmpty(json)) {
            userDTO = JSON.parseObject(json, UserDTO.class);
        }

        //初始化申请列表
        initApplyList();
    }

    private void initApplyList() {
        String listApplyUrl = Utils.getWebServerApiUrl(requireActivity(), getString(R.string.api_friend_apply_list));
        SimpleCallBack<List<FriendApplyDTO>> listApplyCB = new SimpleCallBack<List<FriendApplyDTO>>() {
            @Override
            public void onSuccess(List<FriendApplyDTO> list) throws Throwable {
                mAdapter = new FriendApplyAdapter(requireActivity(), userDTO, list);
                binding.rvApplyList.setAdapter(mAdapter);
            }

            @Override
            public void onError(ApiException e) {
                XToastUtils.toast(e.getDisplayMessage());
            }
        };
        Utils.doGet(listApplyUrl, listApplyCB);
    }
}
